//
//  DSDeviceListContainerView.m
//  RONGAO
//
//  Created by ZhangYeKun on 2017/2/27.
//  Copyright © 2017年 wei wang. All rights reserved.
//

#import "DSDeviceListContainerView.h"
@interface DSDeviceListContainerView ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation DSDeviceListContainerView

- (instancetype)init{
    
    if ([super init]) {
        
        [self masLayoutSubview];
        
    }
    
    return self;
    
}



#pragma mark - --------------------------UITableView dataSource--------------------------

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.IPcameraDeviceList.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
        
    }
    
   

    DoorSystemModel *dev = self.IPcameraDeviceList[indexPath.row];
    if (dev != nil) {
        
        cell.textLabel.text = dev.uid;
        cell.textLabel.backgroundColor = [UIColor clearColor];
        cell.detailTextLabel.text = dev.ip;
        cell.detailTextLabel.textColor = [UIColor grayColor];
        
        cell.detailTextLabel.backgroundColor = [UIColor clearColor];
    
    }

    /*
     不知道要干嘛的
    BOOL isDeviceExist = NO;
    
    for (Camera *camera in camera_list) {
        if ([camera.uid isEqualToString:dev.uid]) {
            isDeviceExist = YES;
            break;
        }
    }
    */
  

    
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
    
}
#pragma mark - --------------------------UITableView delegate--------------------------
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 49.f;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    return 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return CGFLOAT_MIN;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        
    }
    cell.textLabel.text = [NSString stringWithFormat:@"已经搜索到%lu个设备",(unsigned long)self.IPcameraDeviceList.count];
    cell.textLabel.font = [UIFont systemFontOfSize:13.f];
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    DoorSystemModel *dev = self.IPcameraDeviceList[indexPath.row];
    if (dev == nil) {
        [SVProgressHUD showErrorWithStatus:@"设备不存在"];
        return;
    }
    if (self.selectIPCamra) {
        
        self.selectIPCamra(dev);

    }
}

- (UITableView *)tableView{
    
    if (!_tableView) {
        
        __weak UIView *superView = self;
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        [superView addSubview:_tableView];
        //        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;//隐藏tableView的分割线
        //        _tableView.scrollEnabled = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.scrollEnabled = NO;
    }
    
    return _tableView;
    
}

- (NSMutableArray *)IPcameraDeviceList{
    
    if (!_IPcameraDeviceList) {
        _IPcameraDeviceList = [NSMutableArray array];
    }
    return _IPcameraDeviceList;
}
- (DSDeviceListBottomView *)bottomView{
    
    if (!_bottomView) {
        
        __weak UIView *superView = self;
        _bottomView = [[DSDeviceListBottomView alloc]init];
        [superView addSubview:_bottomView];
        
    }
    
    return _bottomView;
    
}

- (void)masLayoutSubview{
    
    __weak UIView *superView = self;
    [self.bottomView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(superView.mas_left);
        make.right.mas_equalTo(superView.mas_right);
        make.bottom.mas_equalTo(superView.mas_bottom);
        make.height.mas_equalTo(pxScaleHeight(500));
        
    }];
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
       make.left.right.top.mas_equalTo(superView);
       make.bottom.mas_equalTo(self.bottomView.mas_top);
        
    }];
    
}

@end
